function waitForPlayer() {
  if (typeof videojs === 'undefined') {
    setTimeout(waitForPlayer, 100);
    return;
  }

  console.log('找到 videojs');
  const player = videojs.getPlayers()['video'];
  if (!player) {
    setTimeout(waitForPlayer, 100);
    return;
  }

  console.log('找到播放器实例:', player);
  let userSetSpeed = 1;
  
  if (!player.tech_ || !player.tech_.el_) {
    console.log('等待播放器技术层加载...');
    setTimeout(waitForPlayer, 100);
    return;
  }

  const videoElement = player.tech_.el_;
  console.log('找到视频元素:', videoElement);

  const silentHandle = (e) => {
    e.preventDefault();
    e.stopPropagation();
  };

  try {
    player.off('ratechange');
    videoElement.removeEventListener('ratechange', silentHandle);
    videoElement.addEventListener('ratechange', silentHandle, true);
  } catch(e) {}

  function setPlaybackRate(value) {
    if (value >= 0.1 && value <= 20) {
      try {
        userSetSpeed = value;
        videoElement.playbackRate = value;
      } catch(e) {}
    }
  }

  videoElement.addEventListener('play', function() {
    if (userSetSpeed !== 1) {
      setTimeout(() => {
        setPlaybackRate(userSetSpeed);
      }, 100);
    }
  });

  window.addEventListener('message', function(event) {
    if (event.data && event.data.type === 'SET_SPEED') {
      setPlaybackRate(event.data.speed);
    }
  });

  setInterval(() => {
    if (videoElement.playbackRate !== userSetSpeed) {
      setPlaybackRate(userSetSpeed);
    }
  }, 500);

  try {
    Object.defineProperty(videoElement, 'playbackRate', {
      get: function() {
        return userSetSpeed;
      },
      set: function(value) {
        if (value === 1 && userSetSpeed !== 1) {
          return;
        }
        setPlaybackRate(value);
      },
      configurable: true
    });
  } catch(e) {}
}

waitForPlayer(); 